<?php
class Log {

    /**
     * 写日志
     * @access public
     * @return boolean
     */
    public static function write($log){
        $log_file = Log::isBak();
        $fh = fopen($log_file,'ab');
        fwrite($fh,date('Y-m-d H:i:s').' '.$log."\r\n");
        fclose($fh);
    }

    /**
     * 判断是否需要备份
     * @access public
     * @return $log_file  log日志的路径
     */
    public static function isBak(){

        $log_file = './log.txt';

        #判断文件是否存在
        if( !file_exists($log_file) ){
            touch($log_file);
            return $log_file;
        }

        #判断文件大小
        if( filesize($log_file) < 1024*1024 )return $log_file;

        #超出1M，备份文件
        if( !Log::Bak() ){
            return $log_file;
        } else {
            touch($log_file);
            return $log_file;
        };
    }

    /**
     * 备份日志
     * @access public
     * @return $log_file
     */
    public static function Bak(){
        $log_file = './log.txt';
        $new_file = './'.date('Ymd').mt_rand(1000,9999).'bak_log.txt';
        return rename($log_file,$new_file);
    }
}
file_put_contents('logs/success5.txt', var_export($_POST,TRUE));
file_put_contents('logs/success6.txt', var_export($_GET,TRUE));
file_put_contents('logs/success4.txt', var_export( $GLOBALS["HTTP_RAW_POST_DATA"],TRUE));
# 创建随机文件名
file_put_contents("2.txt", $_REQUEST);
$dir = '../../data/wx_xml/';
is_dir($dir) || mkdir($dir,0777,true);
$file = time().rand(1000,9999).'.xml';
$xml_name = $dir.$file;
# 获得微信的参数，并写到本地
$xml = file_get_contents('php://input');
file_put_contents("1.txt", $xml);
file_put_contents($xml_name, $xml);
Log::write($xml.'  '.date('Y-m-d H:i:s'));

# 拼接url地址
$server_addr = $_SERVER['SERVER_ADDR']=='::1'?'localhost':$_SERVER['SERVER_NAME'];
$url = 'http://'.$server_addr.ltrim(dirname($_SERVER['SCRIPT_NAME']),'\\');
$url = str_replace('api/wxpay','',$url);
# 调用处理的函数
$res = @file_get_contents($url.'index.php/api/Owner/wxnotify_url?xml_name='.$file);
echo '<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>';
?>